package com.yq.core.util.crawler;

import com.alibaba.fastjson.JSON;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.HashMap;
import java.util.Map;

public class ChinaInfoCrawler {

    public static void main(String[] args) {

        //getHtml("http://www.stats.gov.cn/tjsj/tjbz/xzqhdm/201703/t20170310_1471429.html");
        getHtml2("https://magiccards.info/xln/cn/3.html");

        for(int i=0; i<300; i++) {

        }

    }

    public static void getHtml2(String url) {
        try {

            javax.net.ssl.TrustManager[] trustAllCerts = new javax.net.ssl.TrustManager[1];
            javax.net.ssl.TrustManager tm = new miTM();
            trustAllCerts[0] = tm;
            javax.net.ssl.SSLContext sc = javax.net.ssl.SSLContext
                    .getInstance("SSL");
            sc.init(null, trustAllCerts, null);
            javax.net.ssl.HttpsURLConnection.setDefaultSSLSocketFactory(sc
                    .getSocketFactory());

            Document doc = Jsoup.connect(url).get();

            Elements elements = doc.select("table");
            //for(int i=0; i<elements.size(); i++) {
                Element element = elements.get(3);
                String msoNormal = element.toString();

                System.out.println(element.getElementsByTag("img").get(0));
                System.out.println("=================================================");
                System.out.println(element.getElementsByTag("p"));
                System.out.println("=================================================");
            //}

            /*Elements elements = doc.select("p.MsoNormal");
            for(int i=0; i<elements.size(); i++) {
                Element element = elements.get(i);
                String msoNormal = element.toString();

                String infoStr = msoNormal.replaceAll("<b>", "")
                        .replaceAll("</b>", "")
                        .replaceAll("　", "")
                        .replaceAll(" <", "<")
                        .replaceAll("&nbsp;", "")
                        .replaceAll("<span style=\"font-family: 宋体\"></span>", "")
                        .replaceAll("<span></span>", "");

                //System.out.println(infoStr);
                Elements es = Jsoup.parse(infoStr).select("span");
                System.out.println("(\""+es.get(0).text() + "\",  \"" + es.get(1).text()+"\"),");

            }*/
        } catch (Exception e) {
            e.printStackTrace();
        }
    }




    static class miTM implements javax.net.ssl.TrustManager,
            javax.net.ssl.X509TrustManager {
        public java.security.cert.X509Certificate[] getAcceptedIssuers() {
            return null;
        }

        public boolean isServerTrusted(
                java.security.cert.X509Certificate[] certs) {
            return true;
        }

        public boolean isClientTrusted(
                java.security.cert.X509Certificate[] certs) {
            return true;
        }

        public void checkServerTrusted(
                java.security.cert.X509Certificate[] certs, String authType)
                throws java.security.cert.CertificateException {
            return;
        }

        public void checkClientTrusted(
                java.security.cert.X509Certificate[] certs, String authType)
                throws java.security.cert.CertificateException {
            return;
        }
    }

    public static void getHtml(String url) {
        try {
            Document doc = Jsoup.connect(url).get();

            Elements elements = doc.select("p.MsoNormal");
            for(int i=0; i<elements.size(); i++) {
                Element element = elements.get(i);
                String msoNormal = element.toString();

                String infoStr = msoNormal.replaceAll("<b>", "")
                    .replaceAll("</b>", "")
                    .replaceAll("　", "")
                    .replaceAll(" <", "<")
                    .replaceAll("&nbsp;", "")
                    .replaceAll("<span style=\"font-family: 宋体\"></span>", "")
                    .replaceAll("<span></span>", "");

                //System.out.println(infoStr);
                Elements es = Jsoup.parse(infoStr).select("span");
                System.out.println("(\""+es.get(0).text() + "\",  \"" + es.get(1).text()+"\"),");

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
